package captcha_api

import (
	"blogx_server/common/res"
	"blogx_server/global"
	"github.com/gin-gonic/gin"
	"github.com/mojocn/base64Captcha"
)

type CaptchaApi struct{}

type CaptchaResponse struct {
	CaptchaId   string `json:"captchaID"`
	CaptchaCode string `json:"captchaCode"`
}

func (CaptchaApi) CaptchaView(c *gin.Context) {
	var driver base64Captcha.Driver
	var driverString base64Captcha.DriverString
	captchaConfig := base64Captcha.DriverString{
		Height:          60,
		Width:           200,
		NoiseCount:      100,
		ShowLineOptions: 2 | 4,
		Length:          4,
		Source:          "1234567890",
		//BgColor: &color.RGBA{
		//	R: 3,
		//	G: 102,
		//	B: 214,
		//	A: 125,
		//},
	}
	driverString = captchaConfig
	driver = driverString.ConvertFonts()
	captcha := base64Captcha.NewCaptcha(driver, global.CaptchaStore)
	lid, lb64s, lerr := captcha.Generate()
	//code := stores.Get(lid, true)
	if lerr != nil {
		res.FailWithMsg("图片验证码生成失败", c)
		return
	}
	res.OkWithData(CaptchaResponse{
		CaptchaId:   lid,
		CaptchaCode: lb64s,
	}, c)
}
